package jfinal.demo.company;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import jfinal.demo.common.model.Company;

public class CompanyController extends Controller {
    static CompanyService companyService = new CompanyService();

    public void index() {
        setAttr("pageList", companyService.paginate(getParaToInt(0, 1), 10));
    }

    public void add() {
    }


    public void addAction() {
        if (companyService.add(this)) {
//            //添加成功
            redirect("/company");
        } else {
//            //添加失败
            keepModel(Company.class);
            setAttr("msg", "添加失败");
            render("/company/add.html");
        }
    }

    public void delete() {
        if (companyService.delete(this)) {
//            //删除成功
            redirect("/company");
        } else {
            keepModel(Company.class);
            setAttr("msg", "编辑错误");
            render("/index.html");
        }
    }

    public void edit() {
        setAttr("company", companyService.findById(getParaToInt()));
    }

    public void updata() {
        if (companyService.updata(this)) {
            redirect("/company");
        } else {
            keepModel(Company.class);
            setAttr("msg", "编辑错误");
            render("/company/edit.html");
//            redirect("/company");
        }
    }
}
